package com.sto.stoapp.manager;

import java.util.Hashtable;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

/**
 * cup唤醒锁,锁住不进入待机
 * 
 * @author Administrator
 * 
 */
public class AlarmAlertWakeLockManager {

	private static PowerManager.WakeLock sCpuWakeLock;

	private static Hashtable<String, String> wakeTask = new Hashtable<String, String>();

	/**
	 * 锁定cpu不让进入待机
	 * 
	 * @param context
	 */
	@SuppressLint("Wakelock")
	public static void acquireCpuWakeLock(Context context, String task) {
		wakeTask.put(task, task);
		if (sCpuWakeLock != null) {
			return;
		}

		PowerManager pm = (PowerManager) context
				.getSystemService(Context.POWER_SERVICE);

		sCpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
				| PowerManager.ACQUIRE_CAUSES_WAKEUP
				| PowerManager.ON_AFTER_RELEASE, "AlarmClock");
		sCpuWakeLock.acquire();
		Log.d("AlarmAlertWakeLock=", "/******start*********/");
	}

	/**
	 * 释放锁
	 */
	public static void releaseCpuLock(String task) {
		if (wakeTask.contains(task)) {
			wakeTask.remove(task);
		}
		if (sCpuWakeLock != null && wakeTask.size() == 0) {
			sCpuWakeLock.release();
			sCpuWakeLock = null;
			Log.d("AlarmAlertWakeLock=", "/******release*******/");
		}
	}

}
